package com.tricode.ile.training.task.mite;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Properties;

public class PersonAgeSort {

    public static void main(String[] args) throws IOException {
        Properties properties = getPropertiesFromFile("test.properties");
        List<PersonForAgeSort> persons = generatePersonsFromFile(getPropertyFromProperties(properties, "input.file"));
        sortByAge(persons);
        writePersonsToFile(persons, getPropertyFromProperties(properties, "output.file"));
    }

    private static String getPropertyFromProperties(Properties properties, String property) {
         return properties.getProperty(property);
    }

    private static Properties getPropertiesFromFile(String propertiesFile) throws IOException {
        Properties properties = new Properties();
        properties.load(new FileInputStream(propertiesFile));
        return properties;
    }

    private static void writePersonsToFile(List<PersonForAgeSort> persons, String outputDir) throws IOException {
        File file = new File(outputDir);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        for(PersonForAgeSort p : persons) {
            fileOutputStream.write(p.toString().getBytes());
        }
        fileOutputStream.flush();
        fileOutputStream.close();
    }

    private static List<PersonForAgeSort> generatePersonsFromFile(String filePath) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
        List<PersonForAgeSort> persons = new ArrayList<PersonForAgeSort>();
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            String[] personArray = line.split("\\s+");
            PersonForAgeSort p = createAPerson(personArray);
            persons.add(p);
        }
        return persons;
    }

    private static PersonForAgeSort createAPerson(String[] personArray) {
        String name = personArray[0];
        String lastName = personArray[1];
        int age = Integer.parseInt(personArray[2]);
        return new PersonForAgeSort(name, lastName, age);
    }

    private static void sortByAge(List<PersonForAgeSort> persons) {
        Collections.sort(persons, new Comparator<PersonForAgeSort>() {
            @Override
            public int compare(PersonForAgeSort person1, PersonForAgeSort person2) {
                if (person1.getAge() > person2.getAge()) {
                    return 1;
                } else if (person1.getAge() < person2.getAge()) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
    }

}

class PersonForAgeSort{

    private String name;
    private String lastName;
    private int age;

    PersonForAgeSort(String name, String lastName, int age) {
        this.name = name;
        this.lastName = lastName;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return name +" "+ lastName +" "+ age + "\n";
    }
}
